﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Whoever.Website.BusinessObject;

namespace Whoever.Website.Site
{
    public partial class Contact : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            {
                ClearForm();
            }
            LoadControls();
        }

        #region METHODS

        #region Operations

        protected void LoadControls()
        {
            if (Master != null)
            {
                var contactButton = ((Site)Master).GetControl("Contact");
                contactButton.Style.Add("border-top","1px solid #4ba7d7");
                contactButton.Click += Contact_Click;
            }
        }

        protected void Send()
        {
            SendMessage();
            ClearForm();
            ShowMessageSended();
        }

        protected void ShowMessageSended()
        {
            MessageSended.Style.Add("display","");
        }

        protected void ClearForm()
        {
            TextBoxName.Text = String.Empty;
            TextBoxSurname.Text = String.Empty;
            TextBoxEmail.Text = String.Empty;
            TextBoxPhone.Text = String.Empty;
            TextBoxSubject.Text = String.Empty;
            TextBoxMessage.Text = String.Empty;
            MessageSended.Style.Add("display","none");
            CheckFields.Style.Add("display","none");
        }

        protected void SendMessage()
        {
            var message = new Message();
            var messageManager = new BusinessLogic.MessageManager();

            message.SenderName = TextBoxName.Text;
            message.SenderSurname = TextBoxSurname.Text;
            message.SenderEmail = TextBoxEmail.Text;
            message.SenderPhone = TextBoxPhone.Text;
            message.Subject = TextBoxSubject.Text;
            message.Text = TextBoxMessage.Text;

            messageManager.SendEmail(message);

        }

        #endregion

        #region Validations

        protected bool ValidForm()//Validar email
        {
            //Campos obligatorios
            var obligatoriesFields = new List<string> {TextBoxName.Text, TextBoxEmail.Text, TextBoxMessage.Text};

            return Framework.Utilities.TextValidator.NotNull(obligatoriesFields);
        }

        #endregion


        #endregion

        #region EVENTS

        protected void Contact_Click(object sender, EventArgs e)
        {
            ClearForm();
        }


        #endregion

        protected void ButtonSend_Click(object sender, EventArgs e)
        {
            Send();
        }
    }
}